<script setup>
	import { onMounted, ref } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import { http } from '@/utils/http.js'
	import { parseTime } from '@/utils/index.js'

	const money = ref(0)
	const boxId = ref(0)
	const currentChoose = ref(-1)
	const couponList = ref([])
	async function getCouponList() {
		const res = await http.request({
			url: '/api/user/myCoupon',
			method: 'POST',
			data: {
				page: 1,
				limit: 40,
				status: 0,
				is_use: 1,
				money: money.value,
				id: boxId.value,
			},
		})
		console.log(res)
		couponList.value = res.data
	}

	//查看规则
	const rulepopup = ref()
	const ruleContent = ref('')
	function checkRules(index) {
		rulepopup.value.open('center')
		console.log(couponList.value[index].box_name.length)
		if (couponList.value[index].box_name == 0) {
			ruleContent.value =
				'当前优惠卷在所有的天选之人和无限冲锋中都可以使用噢，小主快去试试吧'
		} else {
			// const str=(couponList.value[index].box_name).join(,)
			ruleContent.value = `当前优惠卷可适用范围：${couponList.value[index].box_name}`
		}
	}

	// 选择
	const couponItem = ref({id:0})
	function radioChange(evt) {
		console.log(evt.detail.value);
		currentChoose.value = evt.detail.value
		couponList.value.forEach((item) => {
			if (item.id == currentChoose.value) {
				couponItem.value = item
			}
		})
	}
	// 确定选择优惠券
	function determine() {
		uni.$emit('thecoupon', { coupon: couponItem.value })
		uni.navigateBack({
			delta: 1,
		})
	}

	function allreadyNow() {
		rulepopup.value.close('center')
	}

	onMounted(() => {})
	onLoad((option) => {
		money.value = option.money
		boxId.value = option.id
		getCouponList()
		if (option.couponid != -1) {
			currentChoose.value = option.couponid
			couponList.value.forEach((item) => {
				if (item.id == currentChoose.value) {
					couponItem.value = item
				}
			})
		}
	})
</script>

<template>
	<view style="padding-bottom: 130rpx;">
		<!-- //优惠券列表 -->
		<radio-group @change="radioChange">
			<view
				class="status-list"
				v-for="(item, index) in couponList"
				:key="item.id"
			>
				<view
					class="coupon-money"
					:class="{ already: item.status == 2 || item.status == 3 }"
				>
					<view class="amount" v-if="item.type == 3">
						{{ item.discount * 10 }}折
					</view>
					<view class="amount" v-else>
						<text style="font-size: 32rpx">￥</text
						>{{ Number(item.money).toFixed(2) }}
					</view>
					<view class="way" v-if="item.type == 1"
						>满{{ Number(item.m_money).toFixed(2) }}元可用</view
					>
					<view class="way" v-if="item.type == 2">无门槛</view>
				</view>
				<view class="coupon-content">
					<view
						class="title"
						:class="{
							'allready-color':
								item.status == 2 || item.status == 3,
						}"
						>{{ item.title }}</view
					>
					<view
						class="time"
						:class="{
							'allready-color':
								item.status == 2 || item.status == 3,
						}"
						>有效期至{{
							parseTime(item.pass_time, '{y}-{m}-{d}')
						}}</view
					>
					<view
						@click="checkRules(index)"
						class="rule"
						:class="{
							'allready-color':
								item.status == 2 || item.status == 3,
						}"
						>规则></view
					>
				</view>
				<view class="coupon-name">
					<view class="goto-use"
						><radio
							:value="item.id"
							:checked="item.id === currentChoose"
					/></view>
				</view>
			</view>
		</radio-group>
		<view class="coupon-button">
			<view class="button" @click="determine"> 确认选择 </view>
		</view>
		<!-- 规则弹出层 -->
		<uni-popup ref="rulepopup" type="center">
			<view class="rulepopup-content">
				<view class="rule-content-title"> 使用规则 </view>
				<view class="rule-content-content">
					{{ ruleContent }}
				</view>
				<view class="rule-content-button" @click="allreadyNow">
					我知道了
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<style lang="scss" scoped>
	page {
		background-color: #fff;
		font-family: OPPOSans;
	}
	.status {
		justify-content: center;
		align-items: center;
		display: flex;
		flex-direction: row;
		margin: 20rpx auto;
		height: 100rpx;
		text-align: center;
		width: 95vw;
	}
	.available {
		width: 156rpx;
		height: 48rpx;
		font-weight: 400;
		background-color: #f7f7f7;
		color: #3d3d3d;
		border-radius: 300rpx;
		text-align: center;
		margin-left: 30rpx;
		line-height: 48rpx;
		font-size: 24rpx;
	}
	.notuse {
		background-color: $all-color;
		color: #fff;
	}
	.status-list {
		display: flex;
		align-items: center;
		width: 95vw;
		margin: 30rpx auto;
		background-color: #fff;
		height: 200rpx;
		border-radius: 10rpx;
		box-shadow: 5rpx 5rpx 10rpx #e5e5e6;
		&:last-child {
			margin-bottom: 100rpx;
		}
	}
	.coupon-money {
		height: 146rpx;
		width: 176rpx;
		background-color: #7863ff;
		margin-left: 30rpx;
		border-radius: 10rpx;
		color: #fff;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin-right: 20rpx;
		flex-shrink: 0;
	}
	.already {
		background-color: #cdcdcd !important;
	}
	.allready-color {
		color: #cdcdcd !important;
	}
	.amount {
		font-size: 40rpx;
	}
	.way {
		line-height: 30rpx;
		font-size: 22rpx;
		font-weight: 400;
		text-align: center;
	}
	.coupon-content {
		flex-shrink: 0;
	}
	.coupon-name {
		border-radius: 10rpx;
		color: #fff;
		display: flex;
		flex-direction: column;
		margin-top: 20rpx;
		height: 140rpx;
		width: 248rpx;
		padding-right: 22rpx;
		box-sizing: border-box;
		background-color: #fff;
		margin: 30rpx auto;
		justify-content: center;
		align-items: flex-end;
		margin-left: 30rpx;
	}
	.coupon-already-use {
		width: 128rpx;
		height: 128rpx;
		background: url('../../static/allready-use.png') no-repeat;
		background-size: contain;
		margin-left: 114rpx;
	}

	.coupon-already-passtime {
		width: 134rpx;
		height: 102rpx;
		background: url('../../static/allready-passtime.png') no-repeat;
		background-size: contain;
		margin-left: 114rpx;
	}
	.title {
		font-size: 32rpx;
		color: #3d3d3d;
		font-weight: 500;
	}
	.time {
		font-size: 22rpx;
		color: #9b9b9d;
	}
	.rule {
		font-size: 22rpx;
		color: #9b9b9d;
		margin-top: 30rpx;
	}
	.goto-use {
	}
	.coupon-button {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		position: fixed;
		bottom: 70rpx;
		.button {
			width: 704rpx;
			height: 82rpx;
			border-radius: 41rpx;
			background-color: $all-color;
			font-size: 28rpx;
			line-height: 82rpx;
			text-align: center;
			color: #fff;
		}
	}
	.rulepopup-content {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 600rpx;
		height: 500rpx;
		padding: 50rpx 46rpx;
		box-sizing: border-box;
		background-color: #fff;
		border-radius: 36rpx;
		.rule-content-title {
			margin-bottom: 54rpx;
			font-size: 40rpx;
			text-align: center;
		}
		.rule-content-content {
			height: 202rpx;
			box-sizing: border-box;
			font-size: 24rpx;
			color: #9b9b9d;
		}
		.rule-content-button {
			width: 460rpx;
			height: 80rpx;
			margin-top: 20rpx;
			box-sizing: border-box;
			border-radius: 40rpx;
			background-color: $all-color;
			text-align: center;
			line-height: 80rpx;
			font-size: 26rpx;
			color: #fff;
		}
	}
</style>
